﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TY.Utilities
{
    /// <summary>
    /// 返回消息
    /// </summary>
    public class JsonMessage
    {
        /// <summary>
        /// 结果编码
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 结果消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 返回数据(对象)
        /// </summary>
        public object Data { get; set; }
        /// <summary>
        /// 返回数据(列表)
        /// </summary>
        public ArrayList Datas { get; set; }

        public static JsonMessage Get(string json)
        {
        
            JsonMessage jm = new JsonMessage();
            Dictionary<string, object> ht = new Dictionary<string, object>();
          
            try
            {
                ht = (Dictionary<string, object>)JSON.Decode(json);
            }
            catch (Newtonsoft.Json.JsonReaderException ex)
            {
                throw new BaseException.ApiException(json, ex);
            }
            jm.Status = Utils.ObjToInt(ht["status"], 0);
            jm.Message = Utils.ObjectToStr(ht["message"]);
            if (ht.ContainsKey("data"))
            {
                if (ht["data"] is Dictionary<string, object>)
                {
                    jm.Data = (Dictionary<string, object>)ht["data"];
                }
                else
                {
                    jm.Data = ht["data"];
                }
               
            }
            if (ht.ContainsKey("datas"))
            {
                jm.Datas = (ArrayList)ht["datas"] ?? new ArrayList();
            }
            return jm;
        }

        //public static JsonMessage Get(string json)
        //{
        //    JsonMessage jm = new JsonMessage();
        //    //Dictionary<string, object> ht = new Dictionary<string, object>();
        //    dynamic ht;
        //    try
        //    {
        //        ht = JSON.Decode(json);
        //    }
        //    catch (Newtonsoft.Json.JsonReaderException ex)
        //    {
        //        throw new BaseException.ApiException("WebServer数据获取错误！", ex);
        //    }
        //    jm.Status = Utils.ObjToInt(ht.status, 0);
        //    jm.Message = Utils.ObjectToStr(ht.message);
        //    if (((IDictionary<string, object>)ht).ContainsKey("data"))
        //    {
        //        jm.Data = ht.data;

        //    }
        //    else
        //    {
        //        jm.Datas = ht.datas;
        //    }
        //    return jm;
        //}
    }
}
